<?php

class SiteController extends BackEndController {
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function  actionLogin() {
        $this->redirect(HSUrl::backendLoginUrl());
    }

    public function accessRules() {
        return array(
            array(
                'allow',
                'actions' => array('login', 'logout'),
                'users' => array('*'),
            ),
            array(
                'allow',
                'actions' => array(
                    'index', 'view', 'create', 'update', 'admin', 'delete', 'task'
                ),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex() {
        $this->layoutPageTitle = 'Trang chính';

        $user = User::model()->findByPk(Yii::app()->user->id);

        if ($user == null) throw new CHttpException(999, 'Invalid request');

        $arr = HSCommon::getCurrentDay();

        $tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . $arr['enum'] . "%", 'all' => '%' . DayEnum::all . '%')
        ));
        $this->render('index',
            array('task' => $tasks, 'day' => $arr['name']));

    }

    public function actionTask() {
        $user = User::model()->findByPk(Yii::app()->user->id);

        if ($user == null) throw new CHttpException(999, 'Invalid request');

        $mo_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::monday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $tu_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::tuesday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $we_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::wednesday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $th_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::thursday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $fr_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::friday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $sa_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::saturday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $su_tasks = Task::model()->findAll(array(
            'select' => '*',
            'condition' => '(user_id = 0 or user_id = :user_id) and (day like :day or day like :all)',
            'params' => array('user_id' => $user->id, 'day' => "%" . DayEnum::sunday . "%", 'all' => '%' . DayEnum::all . '%')
        ));

        $this->render('task',
            array(
                'mo_tasks' => $mo_tasks,
                'tu_tasks' => $tu_tasks,
                'we_tasks' => $we_tasks,
                'th_tasks' => $th_tasks,
                'fr_tasks' => $fr_tasks,
                'sa_tasks' => $sa_tasks,
                'su_tasks' => $su_tasks,
                'user' => $user
            ));
    }

    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('index', $error);
        }
    }

}